package com.lmh.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lmh.entity.Article;
import com.lmh.entity.ArticleAttachment;
import com.lmh.entity.ArticleTag;
import com.lmh.mapper.ArticleAttachmentDao;
import com.lmh.mapper.ArticleDao;
import com.lmh.mapper.ArticleTagDao;
import com.lmh.utils.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleService {

    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private ArticleAttachmentDao articleAttachmentDao;
    @Autowired
    private ArticleTagDao articleTagDao;

    @Transactional
    public int create(Article article) {
        int row = articleDao.create(article);
        article.getFileList().forEach(entity -> {
            ArticleAttachment articleAttachment = new ArticleAttachment();
            // 需要在Mapper文件中指定使用自动生成的id生成策略，否则不会获取id
            articleAttachment.setArticleId(article.getId());
            articleAttachment.setDescription(entity.get("name") + "");
            articleAttachment.setUrl(entity.get("url") + "");
            articleAttachmentDao.create(articleAttachment);
        });
        article.getTagList().forEach(x -> {
            ArticleTag articleTag = new ArticleTag();
            articleTag.setArticleId(article.getId());
            articleTag.setTagId(x);
            articleTagDao.create(articleTag);
        });
        return row;
    }

    @Transactional
    public int delete(Integer id) {
        int row = articleDao.delete(Maps.build(id).getParamMap());
        // 级联删除
        articleAttachmentDao.delete(Maps.build().add("articleId",id).getParamMap());
        articleTagDao.delete(Maps.build().add("articleId",id).getParamMap());
        return row;
    }

    @Transactional
    public int update(Article article) {
        int row = articleDao.update(Maps.build(article.getId()).beanToMapForUpdate(article));
        articleTagDao.delete(Maps.build().add("articleId", article.getId()).getParamMap());
        articleAttachmentDao.delete(Maps.build().add("articleId", article.getId()).getParamMap());
        article.getFileList().forEach(entity -> {
            ArticleAttachment articleAttachment = new ArticleAttachment();
            // 需要在Mapper文件中指定使用自动生成的id生成策略，否则不会获取id
            articleAttachment.setArticleId(article.getId());
            articleAttachment.setDescription(entity.get("name") + "");
            articleAttachment.setUrl(entity.get("url") + "");
            articleAttachmentDao.create(articleAttachment);
        });
        article.getTagList().forEach(x -> {
            ArticleTag articleTag = new ArticleTag();
            articleTag.setArticleId(article.getId());
            articleTag.setTagId(x);
            articleTagDao.create(articleTag);
        });
        return row;
    }

    public PageInfo<Article> query(Article article) {
        if(article != null && article.getPage() != null) {
            PageHelper.startPage(article.getPage(), article.getLimit());
        }
        List<Article> list = articleDao.query(Maps.build().beanToMap(article));
        return new PageInfo<Article>(list);
    }

    public List<Article> top(Article article) {
        PageHelper.startPage(0, article.getLimit(),false);
        return articleDao.top(Maps.build().beanToMap(article));
    }

    public Article detail(Integer id) {
        Article article = articleDao.detail(Maps.build(id).getParamMap());
        List<ArticleTag> articleTagList = articleTagDao.query(Maps.build().add("articleId", id).getParamMap());
        List<ArticleAttachment> articleAttachmentList = articleAttachmentDao.query(Maps.build().add("articleId", id).getParamMap());
        List<Integer> tagList = new ArrayList<>();
        articleTagList.forEach(entity -> {
            tagList.add(entity.getTagId());
        });
        List<Map<String, Object>> attachmentList = new ArrayList<>();
        articleAttachmentList.forEach(entity -> {
            Map<String, Object> map = new HashMap<>();
            map.put("name", entity.getDescription());
            map.put("url", entity.getUrl());
            attachmentList.add(map);
        });
        article.setTagList(tagList);
        article.setFileList(attachmentList);
        return article;
    }

    public int count(Article article) {
        return articleDao.count(Maps.build().beanToMap(article));
    }

}
